<template>
  <el-dialog
    ref="refDlg"
    :title="formData.id ? '编辑' : '新增'"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="600px"
    @close="closeDialog"
  >
    <div>
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="评价类型:" prop="decorateName">
              <el-input v-model="detailData.decorateName" disabled placeholder="请输入评价类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评价类别:" prop="categoryName">
              <el-input v-model="detailData.categoryName" disabled placeholder="请输入评价类别" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="评价项目:" prop="name">
              <el-input v-model="detailData.name" disabled placeholder="请输入评价项目" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分值:" prop="score">
              <el-input v-model.number="formData.score" placeholder="请输入分值" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="启用状态:" prop="enable">
          <el-radio-group v-model="formData.enable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="条目内容:" prop="context">
          <el-input v-model="formData.context" type="textarea" placeholder="请输入条目内容" />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveEvaluateItem } from '@/api/marketService/evaluateItemSetting'
export default {
  name: 'EditForm',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        context: '',
        enable: '',
        id: null,
        projectId: '',
        score: ''
      },
      formRules: {
        context: [{ required: true, message: '请输入', trigger: 'change' }],
        score: [{ required: true, message: '请输入', trigger: 'change' }],
        enable: [{ required: true, message: '请选择', trigger: 'change' }]
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    const data = this.detailData
    if (data && data.id) {
      const { context, enable, id, projectId, score } = data
      this.formData = { context, enable, id, projectId, score }
    } else {
      this.formData.projectId = this.detailData.projectId
    }
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        context: '',
        enable: '',
        id: null,
        projectId: '',
        score: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          this.submiting = true
          saveEvaluateItem(this.formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.closeDialog()
              const isNew = !this.formData.id // 是否新增数据 true (新增则刷新列表到第一页)/ false (非新增则刷新当前页)
              this.$emit('success', isNew)
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
